package com.bw.controller;

import com.alibaba.excel.EasyExcel;
import com.bw.entity.Bing;
import com.bw.entity.Dict;
import com.bw.entity.Zxt;
import com.bw.mapper.StudentMapper;
import org.spring.kun.common.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Controller
public class StudentController {

    @Autowired
    StudentMapper studentMapper;

    /**
     *  首页
     * @return
     */
    @RequestMapping("/tu.do")
    public String tu(Model model){
        // 给小组下拉框赋值
      List<Dict> groupList =   studentMapper.selectGroup();
        model.addAttribute("groupList",groupList);
        return "tu";
    }

    /**
     * 获取  折线图 柱状图  需要的数据
     * @return
     */
    @ResponseBody
    @RequestMapping("/zxt.do")
    public  List<Zxt> zxt(Integer groupId){

        List<Zxt> zzt = studentMapper.zzt(groupId);

        return zzt;
    }
    /**
     * 获取柱状图需要的数据
     * @return
     */
    @ResponseBody
    @RequestMapping("/bing.do")
    public  List<Bing> bing(Integer groupId){

        List<Bing> bing = studentMapper.bing(groupId);

        return bing;
    }

    /**
     * 导出Excel
     * @param groupId
     */
    @RequestMapping("/excel.do")
    public void excel(Integer groupId, HttpServletResponse response) throws IOException {
        // 准备好 Excel 需要的数据
        List<Bing> bing = studentMapper.bing(groupId);

        ExcelUtils.exportExcel(bing,"学生数量导出","sheet页1",response);

    }
}
